package book.chapter05.$5_3_7;

import book.util.Constant;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

//使用错误权限信息的ZooKeeper会话访问含权限信息的数据节点
public class AuthSample_Get2 {

    final static String PATH = "/zk-book-auth_test";

    public static void main(String[] args) throws Exception {

        ZooKeeper zookeeper1 = new ZooKeeper(Constant.CONN_STR, 5000, null);
        zookeeper1.addAuthInfo("digest", "foo:true".getBytes());
        zookeeper1.create(PATH, "init".getBytes(), //
                Ids.CREATOR_ALL_ACL, CreateMode.EPHEMERAL);

        ZooKeeper zookeeper2 = new ZooKeeper(Constant.CONN_STR, 50000, null);
        zookeeper2.addAuthInfo("digest", "foo:true".getBytes());
        //由于权限信息一致，所以能够正确返回
        System.out.println(zookeeper2.getData(PATH, false, null));

        ZooKeeper zookeeper3 = new ZooKeeper(Constant.CONN_STR, 50000, null);
        zookeeper3.addAuthInfo("digest", "foo:false".getBytes());
        //由于权限信息不一致，所以导致 NoAuth
        zookeeper3.getData(PATH, false, null);
    }
}